import React from "react";
import {Redirect, Route, Switch} from "react-router-dom";
import loadable from "@loadable/component";

//PAGES
const SignIn = loadable(() => import("./pages/SignIn"));

const Dashboard = loadable(() => import("./pages/Dashboard"));

const IncidentReport = loadable(() => import("./pages/Inspection/IncidentReport"));
const InspectionReport = loadable(() => import("./pages/Inspection/InspectionReport"));
const Reminder = loadable(() => import("./pages/Inspection/Reminder"));

const Inventory = loadable(() => import("./pages/Maintenance/Inventory"));
const LaborCodes = loadable(() => import("./pages/Maintenance/LaborCodes"));
const Request = loadable(() => import("./pages/Maintenance/Request"));
const Scheduler = loadable(() => import("./pages/Maintenance/Scheduler"));
const Workorder = loadable(() => import("./pages/Maintenance/Workorder"));

const FaultList = loadable(() => import("./pages/FaultList"));

const CheckInCheckOut = loadable(() => import("./pages/ToolManagement/CheckInCheckOut"));
const ToolsInventory = loadable(() => import("./pages/ToolManagement/ToolsInventory"));

const Products = loadable(() => import("./pages/Products"));
const Addresses = loadable(() => import("./pages/Addresses"));
const Routes = loadable(() => import("./pages/Routes"));

const Planning = loadable(() => import("./pages/RoutePlanner/Planning"));
const Operations = loadable(() => import("./pages/RoutePlanner/Operations"));
const Finance = loadable(() => import("./pages/RoutePlanner/Finance"));

const Vehicles = loadable(() => import("./pages/Vehicles"));
const TeamManagement = loadable(() => import("./pages/TeamManagement"));
const Site = loadable(() => import("./pages/Site"));
const Account = loadable(() => import("./pages/Account"));

export const pagesWithoutAuthentication = [
    {id: "signIn", path: "/signIn", component: SignIn}
];

export const pagesWithAuthentication = [
    {id: "dashboard", path: "/dashboard", component: Dashboard},

    {id: "inspectionReport", path: "/inspectionReport", component: InspectionReport},
    {id: "incidentReport", path: "/incidentReport", component: IncidentReport},
    {id: "reminder", path: "/reminder", component: Reminder},

    {id: "workorder", path: "/workorder", component: Workorder},
    {id: "request", path: "/request", component: Request},
    {id: "scheduler", path: "/scheduler", component: Scheduler},
    {id: "inventory", path: "/inventory", component: Inventory},
    {id: "laborCodes", path: "/laborCodes", component: LaborCodes},

    {id: "faults", path: "/faults", component: FaultList},

    {id: "checkInCheckOut", path: "/checkInCheckOut", component: CheckInCheckOut},
    {id: "toolsInventory", path: "/toolsInventory", component: ToolsInventory},

    {id: "products", path: "/products", component: Products},
    {id: "addresses", path: "/addresses", component: Addresses},
    {id: "routes", path: "/routes", component: Routes},

    {id: "planning", path: "/planning", component: Planning},
    {id: "operations", path: "/operations", component: Operations},
    {id: "finance", path: "/finance", component: Finance},

    {id: "vehicles", path: "/vehicles", component: Vehicles},
    {id: "team", path: "/team", component: TeamManagement},
    {id: "site", path: "/site", component: Site},
    {id: "settings", path: "/settings", component: Account}
];

export default ({isAuthenticated}) => {
    const pages = isAuthenticated ? pagesWithAuthentication : pagesWithoutAuthentication;
    const getRoutes = () => pages.map((page) => <Route
        key={page.id}
        id={page.id}
        exact path={page.path}
        component={page.component}
    />);

    return <Switch>
        {getRoutes()}
        <Redirect from="/" to={isAuthenticated ? "/dashboard" : "/signIn"}/>
    </Switch>;
};
